<?php
Zend_Loader::loadClass('Lead_Sql_Query_Where_Condition');

/**
 * Where group
 * @package SQL
 * @subpackage Query
 *
 */
class Lead_Sql_Query_Where_Group
{
	private $_query;
	private $_operator;
	private $_whereConditions = array();
	
	private $_whereGroups = array();
	private $_whereGroupOperator = 'AND';
	private $_currentWhereGroup;
			
	public function __construct(Lead_Sql_Query $query, $operator = 'AND')
	{
		$this->_query = $query;
		$this->_operator = $operator;
	}
		
	
	/**
	 * Adds a new where condition
	 *
	 * @param string $fieldName Field name
	 * @param string $operator Operator
	 * @param mixed $value Value
	 * @param boolean $isQuoted Quoted or not.
	 */
	public function addWhereCond($fieldName, $operator, $value, $isQuoted = false)
	{
	    $expField = explode(".", $fieldName);
		$table = array_shift($expField);
		$field = new Lead_Sql_Query_Field($this->_query, $table, array_shift($expField));
		
		if(!$this->_whereGroups)
		  $this->_whereConditions[] = new Lead_Sql_Query_Where_Condition($field, Lead_Sql_Query_Factory::operator($operator), $value, $isQuoted);
		else 
		  $this->getCurrentWhereGroup()->addWhereCond($fieldName, $operator, $value, $isQuoted);
	}
	
	
	
	/**
	 * Returns SQL for where condition.
	 *
	 * @return string Part of an SQL phrase.
	 */
	public function getSQL()
	{
		if(!$this->_whereGroups) {
    	    $phrase  = null;
    		$wheres = array();
    		foreach($this->getWhereConditions() as $whereCondition)
    		{
    			$wheres[] = $whereCondition->getSQL();
    		}
    		
    		if($wheres)
    			$phrase = "(" . implode(" {$this->_operator} ", $wheres) . ")";
		} else {
    		foreach($this->getWhereGroups() as $whereGroup)
            {
                $wherez = $whereGroup->getSQL();
                if($wherez)
                    $wheres[] = $wherez;
            }
        
            if(sizeof($wheres) > 0) {
                $phrase .= " (" . implode(" {$this->_whereGroupOperator} ", $wheres) . ')';
            }
		}
		
	    return $phrase;

	}
	
	
	public function getWhereConditions()
	{
	    return $this->_whereConditions;
	}
	
	
	    /**
    * Adds a where group.
    *
    * @param unknown_type $operator
    * @return WhereGroup
    */
    public function addWhereGroup($operator = 'AND')
    {
                // Zend_Loader::loadClass('Lead_Sql_Query_Where_Group');
        $whereGroup = new Lead_Sql_Query_Where_Group($this->_query, $operator);
        $this->_whereGroups[] = $whereGroup;
        $this->_currentWhereGroup = $whereGroup;
        return $whereGroup;
    }
    
    /**
    * Returns current where group.
    *
    * @return Lead_Sql_Query_Where_Group
    */
    public function getCurrentWhereGroup()
    {
        if(!$this->_whereGroups)
            return $this;
        
        return $this->_currentWhereGroup;
    }
    
        /**
    * Sets operator used between wheregroups.
    *
    * @param unknown_type $operator
    */
    public function setWhereGroupOperator($operator)
    {
        $this->_whereGroupOperator = $operator;
    }
    
    
    /**
    * Gets operator used between wheregroups.
    *
    * @return unknown
    */
    public function getWhereGroupOperator()
    {
    return $this->_whereGroupOperator;
    }

    
        public function getWhereGroups()
    {
        return $this->_whereGroups;
    }


	
}

?>